package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.vo.AttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @SuppressWarnings("all")
    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Long catId,Map<String, Object> params) {

        QueryWrapper<AttrGroupEntity> w = new QueryWrapper<>();

        if (!catId.equals(0L)){
            w.eq("catelog_id",catId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                w
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupVo> withattrByCatlogId(Long catlogId) {

        QueryWrapper<AttrGroupEntity> w = new QueryWrapper<>();
        w.eq("catelog_id",catlogId);

        List<AttrGroupEntity> list = baseMapper.selectList(w);

        List<AttrGroupVo> collect = list.stream().map(item -> {
            QueryWrapper<AttrEntity> w2 = new QueryWrapper<>();
            w2.eq("attr_type", 1);
            w2.eq("attr_group_id", item.getAttrGroupId());
            List<AttrEntity> attrs = attrDao.selectList(w2);

            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);
            attrGroupVo.setAttrs(attrs);
            return attrGroupVo;

        }).collect(Collectors.toList());
        return collect;
    }

}